package com.example.service;

import com.example.context.BaseContext;
import com.example.entity.Address;
import com.example.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressService {
    @Autowired AddressMapper addressMapper;
    //新增地址
    public void addaddress(Address address) {
        addressMapper.addaddress(address);
    }
    //设置默认地址
    public void setdefault(Address address) {
        address.setIsDefault(0);
        addressMapper.updateIsDefaultByUserId(address);

        address.setIsDefault(1);
        addressMapper.update(address);
    }

    //获得用户所有地址
    public List<Address> getAll(Integer userId) {
        return addressMapper.getAll(userId);
    }
    //修改地址
    public void update(Address address) {
        addressMapper.update(address);
    }

    //删除地址
    public void deleteById(Integer id) {
        addressMapper.deleteById(id);
    }

    //根据id查询地址
    public Address getById(Integer id) {
        return addressMapper.getById(id);
    }

    public Address getDefault(Integer userId) {
        return addressMapper.getDefault(userId);
    }
}
